import {ValidationErrors} from '../types/Auth';

export const validateEmail = (email: string): boolean => {
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  return emailRegex.test(email);
};

export const validatePassword = (password: string): boolean => {
  return password.length >= 6;
};

export const validateName = (name: string): boolean => {
  return name.trim().length >= 2;
};

interface ValidationFields {
  email?: string;
  password?: string;
  name?: string;
  confirmPassword?: string;
}

interface ValidationResult {
  isValid: boolean;
  errors: ValidationErrors;
}

export const validateForm = (fields: ValidationFields): ValidationResult => {
  const errors: ValidationErrors = {};

  if (fields.email && !validateEmail(fields.email)) {
    errors.email = '请输入有效的邮箱地址';
  }

  if (fields.password && !validatePassword(fields.password)) {
    errors.password = '密码至少需要6位字符';
  }

  if (fields.name && !validateName(fields.name)) {
    errors.name = '姓名至少需要2位字符';
  }

  if (fields.confirmPassword && fields.password !== fields.confirmPassword) {
    errors.confirmPassword = '密码不一致';
  }

  return {
    isValid: Object.keys(errors).length === 0,
    errors,
  };
};